@file:JvmName("SharedUtil")

package com.opennews.openplatform.myspringbootcore.util

import java.util.Map
import kotlin.random.Random

/**
 * Converts a snake_case string to camelCase.
 */
fun convertSnakeToCamel(snakeCaseString: String): String {
    return Regex("_(.)").replace(snakeCaseString) { matchResult ->
        matchResult.groupValues[1].uppercase()
    }
}

fun convertCamelToSnake(camelCaseString: String): String {
    return camelCaseString
        .replace(Regex("([a-z])([A-Z])"), "$1_$2")
        .lowercase()
}

/**
 * Gets the next random double value in the provided range.
 */
fun nextRandomDoubleInRange(min: Double, max: Double): Double {
    require(min < max) { "Min must be less than max" }
    return Random.nextDouble(min, max)
}

/**
 * Gets the next random int value in provided range
 */
fun nextRandomIntInRange(min: Int, max: Int): Int {
    require(min < max) { "Min must be less than max" }
    return Random.nextInt(min, max)
}

/**
 * Maps the provided map stream to a stream with the value based on the key.
 */
fun <T> mapListMap(source: List<Map<String, Any>>, key: String): List<T?> {
    return source.map { it[key] as? T }
}